package com.mdd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.dto.DishDto;
import com.mdd.mapper.DishMapper;
import com.mdd.pojo.Dish;
import com.mdd.pojo.DishFlavor;
import com.mdd.service.DishFlavorService;
import com.mdd.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 添加菜品到dish表，并且把菜品口味添加到口味表
     * @param dishDto
     */
    @Override
    //操作多张表，开启事务
    @Transactional
    public void saveDish(DishDto dishDto) {
        //添加菜品到菜品表
        super.save(dishDto);

        //得到菜品的口味集合
        List<DishFlavor> flavors = dishDto.getFlavors();
        //给每一个口味的dishId赋值
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        //批量添加口味
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 通过id得到菜品和对应的口味
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查新菜品
        Dish dish = getById(id);
        //构造条件构造器
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        //查找这个菜品的口味
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        DishDto dishDto = new DishDto();
        //属性拷贝
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(list);
        return dishDto;
    }

    /**
     * 修改菜品以及其对应的口味
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateDish(DishDto dishDto) {
        //修改菜品
        super.updateById(dishDto);
        //先删除这个菜品原来的口味再插入这个菜品新设置的口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(wrapper);
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 删除菜品和对应口味
     * @param ids
     */
    @Override
    @Transactional
    public void deleteDishAndFlavor(List<Long> ids) {
        //删除菜品
        removeByIds(ids);
        //删除菜品对应的所有口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(queryWrapper);
    }

    /**
     * 修改菜品状态
     * @param status
     * @param ids
     * @return
     */
    @Override
    public boolean updateDishStatus(int status, List<Long> ids) {
        LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
        //当id在这个ids集合里就修改
        updateWrapper.in(Dish::getId,ids).set(Dish::getStatus,status);
        return update(updateWrapper);
    }
}
